package com.feihong.txgw.ui.activity.integral;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.adapter.base.BaseQuickAdapter;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.user.IntegralBackReAdapter;
import com.feihong.txgw.inter.contract.integral.IIntegralContract;
import com.feihong.txgw.model.bean.integral.IntegralBackEntity;
import com.feihong.txgw.tx_precenter.IntegralMainPrecenter;
import com.feihong.txgw.ui.mvp.BaseMVPCompateActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;

import java.util.List;

import butterknife.BindView;

/**
 * 积分待返
 */
public class BackIntegralActivity extends BaseMVPCompateActivity<IIntegralContract.AbIntegralMainPrecenter> implements
        IIntegralContract.IIntegralMainView,
        BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.integralBack_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.integralBack_content_layout)
    RecyclerView mContentLayout;
    /**
     * 没有积分的layout
     */
    @BindView(R.id.integralBack_no_layout)
    LinearLayout mNullIntergal;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectErrorLayout;
    @BindView(R.id.re_query_data_layout)
    LinearLayout mReQueryDataLayout;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadLayout;

    private IntegralBackReAdapter mReAdapter;
    private LinearLayoutManager linearLayoutManager;
    private boolean isHasData = false;

    private boolean isFullPage = false;
    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_back_integral;
    }

    @NonNull
    @Override
    public AbBasePrecenter initPresenter() {
        return IntegralMainPrecenter.create();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setPageTitle("积分待返");
        setBackButtonFinish();
        mRefreshLayout.setColorSchemeColors(Color.rgb(223, 47, 47));
        mNullIntergal.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        linearLayoutManager = new LinearLayoutManager(this);
        mContentLayout.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void bindData() {
        super.bindData();
        mLoadLayout.setVisibility(View.VISIBLE);
        getData();

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isHasData = false;
                mPresenter.loadIntegralDataFirst(BackIntegralActivity.class.getName(), "2");
            }
        });
        mReQueryDataLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConnectErrorLayout.setVisibility(View.GONE);
                mLoadLayout.setVisibility(View.VISIBLE);
                getData();
            }
        });

        initAdapter();
    }

    private void initAdapter() {
        mReAdapter = new IntegralBackReAdapter(R.layout.item_integraldetail);
        mReAdapter.setOnLoadMoreListener(this, mContentLayout);
        mContentLayout.setAdapter(mReAdapter);
    }

    @Override
    public void setBackIntegralData(boolean isFullPage,List<IntegralBackEntity.DataBean.PageListBean> integralBackEntity) {
        this.isFullPage = isFullPage;
        mRefreshLayout.setVisibility(View.VISIBLE);
        mNullIntergal.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        if(isHasData){
            mReAdapter.addData(integralBackEntity);
        }else {
            mReAdapter.setNewData(integralBackEntity);
            isHasData = true;
        }
        if(isFullPage){
            mReAdapter.loadMoreEnd(!isFullPage);
        }else {
            mReAdapter.loadMoreComplete();
        }
    }

    @Override
    public void notData() {
        if (!isHasData) {
            mRefreshLayout.setVisibility(View.GONE);
            mNullIntergal.setVisibility(View.VISIBLE);
            mConnectErrorLayout.setVisibility(View.GONE);
        }
        mReAdapter.loadMoreEnd(!isFullPage);
    }

    @Override
    public void dataOtherError(String errorMsg) {
        finishRefreshing();
        if (!isHasData) {
            mRefreshLayout.setVisibility(View.GONE);
            mNullIntergal.setVisibility(View.VISIBLE);
            mConnectErrorLayout.setVisibility(View.GONE);
        }
        mReAdapter.loadMoreEnd(!isFullPage);
        showSystemToast(errorMsg);
    }

    @Override
    public void loadDataComplated() {
        mLoadLayout.setVisibility(View.GONE);
        finishRefreshing();
    }

    @Override
    public void loadDataError() {
        finishRefreshing();
        mReAdapter.loadMoreFail();
        mLoadLayout.setVisibility(View.GONE);
        if (isHasData) {
            showSystemToast(getString(R.string.connect_service_error));
        } else {
            mRefreshLayout.setVisibility(View.GONE);
            mNullIntergal.setVisibility(View.GONE);
            mConnectErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        getData();
    }

    private void getData() {
        mPresenter.loadBackIntegralData(BackIntegralActivity.class.getName(), "2");
    }

    private void finishRefreshing() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
    }
}
